import { defineStore } from 'pinia';
import axios from '@/plugins/axios';
import { ElMessage } from 'element-plus';

export interface Message {
  id: number;
  title: string;
  content: string;
  studentId: number;
  studentName: string;
  hasUnreadReply: boolean;
  createTime: string;
}

export interface Reply {
  id: number;
  messageId: number;
  teacherId: number;
  teacherName: string;
  content: string;
  createTime: string;
}

export interface MessageCreateRequest {
  studentId: number;
  title: string;
  content: string;
}

export interface MessageUpdateRequest {
  id: number;
  studentId: number;
  title: string;
  content: string;
}

export interface ReplyCreateRequest {
  messageId: number;
  teacherId: number;
  content: string;
}

export const useMessageStore = defineStore('message', {
  state: () => ({
    messages: [] as Message[],
    notifications: [] as Message[],
  }),

  actions: {
    async fetchMessages() {
      try {
        const response = await axios.get('/api/message');
        if (response.data.code === 200) {
          this.messages = response.data.data;
          return this.messages;
        }
        throw new Error(response.data.message || '获取留言列表失败');
      } catch (error: any) {
        ElMessage.error(error.message || '获取留言列表失败');
        throw error;
      }
    },

    async fetchMessageById(id: number) {
      try {
        const response = await axios.get(`/api/message/${id}`);
        if (response.data.code === 200) {
          return response.data.data;
        }
        throw new Error(response.data.message || '获取留言详情失败');
      } catch (error: any) {
        ElMessage.error(error.message || '获取留言详情失败');
        throw error;
      }
    },

    async createMessage(data: MessageCreateRequest) {
      try {
        const response = await axios.post('/api/message', data);
        if (response.data.code === 200) {
          this.messages.push(response.data.data);
          ElMessage.success('创建留言成功');
          return response.data.data;
        }
        throw new Error(response.data.message || '创建留言失败');
      } catch (error: any) {
        ElMessage.error(error.message || '创建留言失败');
        throw error;
      }
    },

    async updateMessage(data: MessageUpdateRequest) {
      try {
        const response = await axios.put('/api/message', data);
        if (response.data.code === 200) {
          const index = this.messages.findIndex(m => m.id === data.id);
          if (index !== -1) {
            this.messages[index] = response.data.data;
          }
          ElMessage.success('更新留言成功');
          return response.data.data;
        }
        throw new Error(response.data.message || '更新留言失败');
      } catch (error: any) {
        ElMessage.error(error.message || '更新留言失败');
        throw error;
      }
    },

    async deleteMessage(id: number) {
      try {
        const response = await axios.delete(`/api/message/${id}`);
        if (response.data.code === 200) {
          this.messages = this.messages.filter(m => m.id !== id);
          ElMessage.success('删除留言成功');
          return response.data.data;
        }
        throw new Error(response.data.message || '删除留言失败');
      } catch (error: any) {
        ElMessage.error(error.message || '删除留言失败');
        throw error;
      }
    },

    async fetchReplies(messageId: number) {
      try {
        const response = await axios.get(`/api/message/${messageId}/reply`);
        if (response.data.code === 200) {
          return response.data.data;
        }
        throw new Error(response.data.message || '获取回复列表失败');
      } catch (error: any) {
        ElMessage.error(error.message || '获取回复列表失败');
        throw error;
      }
    },

    async createReply(data: ReplyCreateRequest) {
      try {
        const response = await axios.post(`/api/message/${data.messageId}/reply`, data);
        if (response.data.code === 200) {
          ElMessage.success('回复成功');
          return response.data.data;
        }
        throw new Error(response.data.message || '回复失败');
      } catch (error: any) {
        ElMessage.error(error.message || '回复失败');
        throw error;
      }
    },

    async deleteReply(replyId: number) {
      try {
        const response = await axios.delete(`/api/message/reply/${replyId}`);
        if (response.data.code === 200) {
          ElMessage.success('删除回复成功');
          return response.data.data;
        }
        throw new Error(response.data.message || '删除回复失败');
      } catch (error: any) {
        ElMessage.error(error.message || '删除回复失败');
        throw error;
      }
    },

    async fetchNotifications(studentId: number) {
      try {
        const response = await axios.get(`/api/message/notify/${studentId}`);
        if (response.data.code === 200) {
          this.notifications = response.data.data;
          return this.notifications;
        }
        throw new Error(response.data.message || '获取通知失败');
      } catch (error: any) {
        throw error;
      }
    },

    async markAsRead(messageId: number) {
      try {
        const response = await axios.post(`/api/message/${messageId}/read`);
        if (response.data.code === 200) {
          this.notifications = this.notifications.filter(n => n.id !== messageId);
          const message = this.messages.find(m => m.id === messageId);
          if (message) {
            message.hasUnreadReply = false;
          }
          return response.data.data;
        }
        throw new Error(response.data.message || '标记已读失败');
      } catch (error: any) {
        ElMessage.error(error.message || '标记已读失败');
        throw error;
      }
    },
  },
});